580D - Kefa and Dishes - CodeForces Solution


bitmasks dp *1800

Please click on ads to support us..

C++ Code:

#include <algorithm>
#include <bits/stdc++.h>
#include <bitset>
#include <cstring>
#include <vector>
#define fast_input ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
using namespace ::std;
#define int long long

int n, m, k;
vector<int> a;
int memo[1<<20][20];

int DP(int last, int bitmask, vector<vector<int>>& c) {
    if (__builtin_popcount(bitmask) == m) return 0;

    if (memo[bitmask][last] != -1) return memo[bitmask][last]; 
    memo[bitmask][last] = 0;

    for (int i =0; i<n; i++) {
        if (!(bitmask&(1<<i))) {
            memo[bitmask][last] = max(memo[bitmask][last], a[i]+c[last][i]+DP(i, bitmask | (1<<i), c));
        }
    }

    return memo[bitmask][last];
}


int32_t main() {
    fast_input;

    cin >> n >> m >> k;

    vector<vector<int>> c (n+1, vector<int>(n+1));
    memset(memo, -1, sizeof(memo));

    for (int i=0; i<n; i++) {
        int num;
        cin >> num;
        a.push_back(num);
    }

    for (int i =0; i<k; i++) {
        int ii, j, ci;
        cin >> ii >> j >> ci;

        c[ii-1][j-1] = ci;
    }

    // for (int i=0; i<100000; i++) {
    //     for (int j=0; j<10; j++) {
    //         cout << memo[i][j] << " ";
    //     }
    //     cout <<  endl;
    // }

    cout << DP(n,0, c) << endl;
    
    return 0;
}


Comments

Submit
0 Comments
More Questions

25A - IQ test
785A - Anton and Polyhedrons
1542B - Plus and Multiply
306A - Candies
1651C - Fault-tolerant Network
870A - Search for Pretty Integers
1174A - Ehab Fails to Be Thanos
1169A - Circle Metro
780C - Andryusha and Colored Balloons
1153A - Serval and Bus
1487C - Minimum Ties
1136A - Nastya Is Reading a Book
1353B - Two Arrays And Swaps
1490E - Accidental Victory
1335A - Candies and Two Sisters
96B - Lucky Numbers (easy)
1151B - Dima and a Bad XOR
1435B - A New Technique
1633A - Div 7
268A - Games
1062B - Math
1294C - Product of Three Numbers
749A - Bachgold Problem
1486B - Eastern Exhibition
1363A - Odd Selection
131B - Opposites Attract
490C - Hacking Cypher
158B - Taxi
41C - Email address
1373D - Maximum Sum on Even Positions